package com.java.poc.pdf;

import com.pdfcrowd.*;
import java.io.*;
import java.util.Scanner;

public class Pdfcrowd {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try
        {
            FileOutputStream fileStream;

            // create an API client instance
            Client client = new Client("rupeshgoyal", "6f7dcbfd4d245a25c0ea3c7d91464b1d");

            String root = "D:\\Java\\MyWorkspace\\Poc\\javapoc\\apppoc";
            String input = "test1.htm";
            File htmlIn = new File(root, input);
            Scanner scanner = new Scanner(htmlIn).useDelimiter("\\Z");
            String htmlContents = scanner.next();

            // convert a web page and save the PDF to a file
            /*fileStream = new FileOutputStream("example.pdf");
            client.convertURI("https://www.google.co.in/", fileStream);
            fileStream.close();*/

            // convert an HTML string and store the PDF into a byte array
            ByteArrayOutputStream memStream  = new ByteArrayOutputStream();
            //String html = "<html><body>In-memory HTML.</body></html>";
            //System.out.println(htmlContents);
            client.convertHtml(htmlContents, memStream);

            String filename = "d:\\test.pdf";
            FileOutputStream output = new FileOutputStream(filename);
            output.write(memStream.toByteArray());
            output.close();

            /*// convert an HTML file
            fileStream = new FileOutputStream("file.pdf");
            client.convertFile("/path/to/local/file.html", fileStream);
            fileStream.close();*/

            // retrieve the number of tokens in your account
            //Integer ntokens = client.numTokens();
        }
        catch(PdfcrowdError why) {
            System.err.println(why.getMessage());
        }
        catch(IOException exc) {
            // handle the exception
        }
	}

}
